package com.ym.common.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @Author wenbo
 * @Date 2020/6/16 17:21
 **/
public class SourceFileUtil {

	/**
	 * 开放接口 appkey 公钥获取  或者 source目录文件获取
	 *
	 * @return
	 * @throws IOException
	 */
	public static String source(String fileName) {
		ClassPathResource classPathResource = new ClassPathResource(fileName);
		InputStream inputStream = null;
		try {
			inputStream = classPathResource.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String read = IoUtil.read(inputStream, Charset.defaultCharset());
		IoUtil.close(inputStream);
		return read;
	}

	/**
	 * restTemplate Header 头设计
	 *
	 * @param appKey
	 * @return
	 * @throws Exception
	 */
	public static HttpHeaders restTemplateHeader(String appKey) {
		String timestamp = String.valueOf(System.currentTimeMillis());
		String str = timestamp + "health";
		String sign = SecureUtil.rsa(null, appKey).encryptBase64(str, KeyType.PublicKey);
		HttpHeaders headers = new HttpHeaders();
		headers.add("appKey", appKey);
		headers.add("sign", sign);
		headers.add("timestamp", timestamp);
		return headers;
	}
}
